﻿package controllers.system;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rbac.models.Oplog;
import rbac.models.User;

import com.et.mvc.BinaryView;
import com.et.mvc.JsonView;
import com.et.mvc.MultipartFile;
import com.et.mvc.TextView;
import com.et.mvc.View;

import com.et.mvc.filter.BeforeFilter;
import com.et.mvc.filter.BeforeFilters;

import controllers.ApplicationController;


public class OplogController extends ApplicationController {
	
	public void index() throws Exception{
    	
	}
	
	public View getOplogItems(int page, int rows, String q,int t, String sort, String order) throws Exception {
		String orderBy = "id desc";
		if (sort != null && order != null){
			orderBy = sort + " " + order;
		}
		String cond = "1=1";
		List<Object> tmpArgs = new ArrayList<Object>();
		
		/**
		if (t != 0){
			cond += " and (parentId = ?)";
			tmpArgs.add(t);
		}
		**/
		if (q != null){
			cond += " and (logfrom like '%"+q+"%' or msg like '%"+q+"%' ) ";			
		}
		

		Object[] args = tmpArgs.toArray();
		
		long total = Oplog.count(Oplog.class, cond, args);
		List<Oplog> items = Oplog.findAll(Oplog.class, cond, args, orderBy, rows, (page-1)*rows);
		
		Map<String,Object> result = new HashMap<String,Object>();
		result.put("total", total);
		result.put("rows", items);
		
		return new JsonView(result);
	}
	
	public View destroyOplog(int id) throws Exception {
		Oplog item = Oplog.find(Oplog.class, id);
		User user = (User) rbac.getCurrentUser();				
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")删除日志，内容为：" + new JsonView(item));									

		item.destroy();
		return new JsonView("success:true");
	}
	
	public View destroyAllOplog() throws Exception {
		User user = (User) rbac.getCurrentUser();		
		List<Oplog> items = Oplog.findAll(Oplog.class, "1=1", null);
		Oplog.addMsg(user.getLogin(), user.getName() + "("+getIpAddr(request)+")清空日志，内容为：" + new JsonView(items));									

		Oplog.destroyAll(Oplog.class, "1=1", null);
		return new JsonView("success:true");
	}
	
}
